package com.mvetc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.mvetc.entity.Movies;
import com.mvetc.entity.PageInfo;
import com.mvetc.util.DBUtil;
import com.mvetc.dao.AdminDao;


public class AdminDaoImpl implements AdminDao{
	public PageInfo<Movies> findByPage(String name,int pageNum, int pageSize) {
		List<Movies> data = new ArrayList<>();
		ResultSet doQuery = DBUtil.doQuery("select movie_id,movie_name,movie_publish_date,movie_picture,movie_rating,movie_status,movie_spell "
				+ "from movie_info where movie_name like ? limit ?,?","%"+name+"%",(pageNum-1)*pageSize,pageSize);
		try {
			while (doQuery.next()) {
				String movieId = doQuery.getString("movie_id");
				String movieName = doQuery.getString("movie_name");
				String publishDate = doQuery.getString("movie_publish_date");
				String picture = doQuery.getString("movie_picture");
				String rating = doQuery.getString("movie_rating");
				String status = doQuery.getString("movie_status");
				String spell = doQuery.getString("movie_spell");
				
				
				Movies movies = new Movies();
				movies.setMovieId(movieId);
				movies.setMovieName(movieName);
				movies.setMoviePublishDate(publishDate);
				movies.setMoviePicture(picture);
				movies.setMovieRating(rating);
				movies.setMovieStatus(status);
				movies.setMovieSpell(spell);

				data.add(movies);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		Integer total = 0;
		// 获取符合当前查询条件的总记录数
		doQuery = DBUtil.doQuery("select count(*) from movie_info where movie_name like ?","%"+name+"%");
		try {
			while (doQuery.next()) {
				total = doQuery.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		PageInfo<Movies> pageInfo = new PageInfo<>(data, pageNum, pageSize, total);
		
		return pageInfo;
	}

	@Override
	public PageInfo<Movies> findAllByPage(int pageNum, int pageSize) {
		List<Movies> data = new ArrayList<>();
		ResultSet doQuery = DBUtil.doQuery("select A.movie_id,A.movie_name,A.movie_publish_date,A.movie_duration,"
				+ "A.movie_picture,A.movie_rating,A.movie_status,A.movie_spell,D.country_name,A.movie_type "
				+ "from movie_info A "
				+ "inner join country_info D on A.country_id=D.country_id "
				+ "limit ?,?",(pageNum-1)*pageSize,pageSize);
		try {
			while (doQuery.next()) {
				String movieId = doQuery.getString("movie_id");
				String movieName = doQuery.getString("movie_name");
				String publishDate = doQuery.getString("movie_publish_date");
				String picture = doQuery.getString("movie_picture");
				String rating = doQuery.getString("movie_rating");
				String status = doQuery.getString("movie_status");
				String spell = doQuery.getString("movie_spell");
				String countryName = doQuery.getString("country_name");
				String time = doQuery.getString("movie_duration");
				String typeName = doQuery.getString("movie_type");
				
				//----------------------------------------------------------------------------

				
				
				Movies movies = new Movies();
				movies.setMovieId(movieId);
				movies.setMovieName(movieName);
				movies.setMoviePublishDate(publishDate);
				movies.setMoviePicture(picture);
				movies.setMovieRating(rating);
				movies.setMovieStatus(status);
				movies.setMovieSpell(spell);
				movies.setCountryName(countryName);;
				movies.setMovieDuration(time);
				movies.setTypeName(typeName);

				data.add(movies);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		Integer total = 0;
		// 获取符合当前查询条件的总记录数
		doQuery = DBUtil.doQuery("select count(*) from movie_info");
		try {
			while (doQuery.next()) {
				total = doQuery.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		PageInfo<Movies> pageInfo = new PageInfo<>(data, pageNum, pageSize, total);
		
		return pageInfo;
	}

	@Override
	public PageInfo<Movies> findAllBySpell(String spell,int pageNum, int pageSize) {
		List<Movies> data = new ArrayList<>();
		ResultSet doQuery = DBUtil.doQuery("select A.movie_id,A.movie_name,A.movie_publish_date,A.movie_duration,"
				+ "A.movie_picture,A.movie_rating,A.movie_status,A.movie_spell,D.country_name,A.movie_type "
				+ "from movie_info A "
				+ "inner join country_info D on A.country_id=D.country_id "
				+ "where A.movie_spell like ? limit ?,?",spell+"%",(pageNum-1)*pageSize,pageSize);
		try {
			while (doQuery.next()) {
				String movieId = doQuery.getString("movie_id");
				String movieName = doQuery.getString("movie_name");
				String publishDate = doQuery.getString("movie_publish_date");
				String picture = doQuery.getString("movie_picture");
				String rating = doQuery.getString("movie_rating");
				String status = doQuery.getString("movie_status");
				String movieSpell = doQuery.getString("movie_spell");
				String countryName = doQuery.getString("country_name");
				String time = doQuery.getString("movie_duration");
				String typeName = doQuery.getString("movie_type");
				
				//----------------------------------------------------------------------------

				
				
				Movies movies = new Movies();
				movies.setMovieId(movieId);
				movies.setMovieName(movieName);
				movies.setMoviePublishDate(publishDate);
				movies.setMoviePicture(picture);
				movies.setMovieRating(rating);
				movies.setMovieStatus(status);
				movies.setMovieSpell(movieSpell);
				movies.setCountryName(countryName);;
				movies.setMovieDuration(time);
				movies.setTypeName(typeName);

				data.add(movies);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		Integer total = 0;
		// 获取符合当前查询条件的总记录数
		 doQuery = DBUtil.doQuery("select count(*) from movie_info where movie_spell like ?",spell+"%");
		try {
			while (doQuery.next()) {
				total = doQuery.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		PageInfo<Movies> pageInfo = new PageInfo<>(data, pageNum, pageSize, total);
		
		return pageInfo;
	}

	@Override
	public int addCount(String movieId) {
		ResultSet doQuery = DBUtil.doQuery("select movie_count from movie_info where movie_id=?",movieId);
		String count=null;
		try {
			while (doQuery.next()) {
				count = doQuery.getString(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i=(Integer.parseInt(count)+1);
		String s=String.valueOf(i);
		return DBUtil.doUpdate("update movie_info set movie_count=? where movie_id=?",s,movieId);
	}
}

